var role = [];

$(function() {
  var listData = {
    "initData": { //默认参数配置
      "page": "1",
      "pageSize": "10"
    },
    "url": site.apiUrl + "/admin/selectAdminInfo", // 数据请求url
    "type": "get",
    // "checkname": "datachk",
    "col": [ //数据列
      { "field": "adminName", "display": "账号" },
      { "field": "name", "display": "昵称" },
      {
        "field": "ifSuperMan",
        "display": "是否是超级管理员",
        "show": [
          { "value": "0", "html": "<span>不是</span>" },
          { "value": "1", "html": "<span>是</span>" }
        ]
      },
      { "field": "webName", "display": "网点名称" },
      // { "field": "creationTime", "display": "创建时间", "fun": "changeDate", "width": "3" }
    ],
    "option": [ // 操作按钮配置
      { "icon": "edit", "tip": "编辑", "url": "add.html?id={{id}}&type=2" },
      { "icon": "del", "tip": "删除", "fun": "deleteBrand('{{id}}')" }
    ]
  }
  $("#adminList").getList(listData);

  //批量删除
  $("#batchDelBtn").click(function() {
    var c = "";
    $("input[name=datachk]:checked").each(function() {
      c += $(this).val() + ',';
    });
    if (c != null && c != '') {
      deleteBrand(c);
    } else {
      ly.toast("请选择删除项");
    }
  });

  // adminCount(); // 统计用户数

});

// 时间戳转换时间
function changeDate(v, o, d) {
  var baseDateTime;
  var timestamp = new Date(d.creationTime);
  baseDateTime = timestamp.toLocaleDateString().replace(/\//g, "-") + " " + timestamp.toTimeString().substr(0, 8)
  if (d.creationTime != null && d.creationTime != '') {
    $(o).html(baseDateTime)
  }
}

/**
 * 统计用户数
 * 用户大于5人,隐藏"新增"按钮
 */
function adminCount() {
  var adminNum = $("#adminList table tr").length;
  if (adminNum > 6) {
    $("#bb1").hide();
  } else {
    $("#bb1").show();
  }
}

//删除
function deleteBrand(id) {
  ly.confirm("确定删除吗？", function(r) {
    var url = site.apiUrl + "/admin/deleteAdmin";
    request({
      url: url,
      data: { "id": id },
      type: "post",
      callback: function(f) {
        if (f.code == 200) {
          ly.toast("删除成功");
          $("#adminList").getList.refresh(); //刷新当前分页
          // window.location.reload();
        } else {
          ly.toast(f.desc || f.msg);
        }
      }
    })
  })
}
